home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / StencilBuffer / StencilDepth / StencilDepth.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  15.7 KB  |  442 lines

  1. //-----------------------------------------------------------------------------
  2. // File: StencilDepth.cpp
  3. //
  4. // Desc: Example code showing how to use stencil buffers to show the depth
  5. //       complexity of a scene.
  6. //
  7. // Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define STRICT
  10. #include <D3DX8.h>
  11. #include "D3DApp.h"
  12. #include "D3DFile.h"
  13. #include "D3DFont.h"
  14. #include "D3DUtil.h"
  15. #include "DXUtil.h"
  16. #include "resource.h"
  17.  
  18.  
  19.  
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Name: class CMyD3DApplication
  23. // Desc: Application class. The base class (CD3DApplication) provides the 
  24. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  25. //       adds functionality specific to this sample program.
  26. //-----------------------------------------------------------------------------
  27. class CMyD3DApplication : public CD3DApplication
  28. {
  29.     CD3DFont* m_pFont;
  30.     BOOL      m_bShowDepthComplexity;
  31.     CD3DFile* m_pFileObject;
  32.  
  33.     LPDIRECT3DVERTEXBUFFER8 m_pBigSquareVB;
  34.  
  35.     VOID    SetStatesForRecordingDepthComplexity();
  36.     VOID    ShowDepthComplexity();
  37.     HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
  38.  
  39.     D3DXMATRIX  m_matWorldMatrix;
  40.  
  41. protected:
  42.     HRESULT OneTimeSceneInit();
  43.     HRESULT InitDeviceObjects();
  44.     HRESULT RestoreDeviceObjects();
  45.     HRESULT InvalidateDeviceObjects();
  46.     HRESULT DeleteDeviceObjects();
  47.     HRESULT Render();
  48.     HRESULT FrameMove();
  49.     HRESULT FinalCleanup();
  50.  
  51. public:
  52.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  53.  
  54.     CMyD3DApplication();
  55. };
  56.  
  57.  
  58.  
  59.  
  60. //-----------------------------------------------------------------------------
  61. // Name: WinMain()
  62. // Desc: Entry point to the program. Initializes everything, and goes into a
  63. //       message-processing loop. Idle time is used to render the scene.
  64. //-----------------------------------------------------------------------------
  65. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  66. {
  67.     CMyD3DApplication d3dApp;
  68.  
  69.     if( FAILED( d3dApp.Create( hInst ) ) )
  70.         return 0;
  71.  
  72.     return d3dApp.Run();
  73. }
  74.  
  75.  
  76.  
  77.  
  78. //-----------------------------------------------------------------------------
  79. // Name: CMyD3DApplication()
  80. // Desc: Application constructor. Sets attributes for the app.
  81. //-----------------------------------------------------------------------------
  82. CMyD3DApplication::CMyD3DApplication()
  83. {
  84.     m_strWindowTitle    = _T("StencilDepth: Displaying Depth Complexity");
  85.     m_bUseDepthBuffer   = TRUE;
  86.     m_dwMinDepthBits    = 16;
  87.     m_dwMinStencilBits  = 4;
  88.  
  89.     m_bShowDepthComplexity = TRUE;
  90.     m_pFont                = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  91.     m_pFileObject          = new CD3DFile();
  92.     m_pBigSquareVB         = NULL;
  93.     
  94.     D3DXMatrixIdentity( &m_matWorldMatrix );
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101. //-----------------------------------------------------------------------------
  102. // Name: OneTimeSceneInit()
  103. // Desc: Called during initial app startup, this function performs all the
  104. //       permanent initialization.
  105. //-----------------------------------------------------------------------------
  106. HRESULT CMyD3DApplication::OneTimeSceneInit()
  107. {
  108.     return S_OK;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. //-----------------------------------------------------------------------------
  115. // Name: FrameMove()
  116. // Desc: Called once per frame, the call is the entry point for animating
  117. //       the scene.
  118. //-----------------------------------------------------------------------------
  119. HRESULT CMyD3DApplication::FrameMove()
  120. {
  121.     // Setup the world spin matrix
  122.     D3DXMatrixRotationAxis( &m_matWorldMatrix, &D3DXVECTOR3(1.0f,1.0f,0.0f), m_fTime/2  );      
  123.  
  124.     return S_OK;
  125. }
  126.  
  127.  
  128.  
  129.  
  130. //-----------------------------------------------------------------------------
  131. // Name: SetStatesForRecordingDepthComplexity()
  132. // Desc: Turns on stencil and other states for recording the depth complexity
  133. //       during the rendering of a scene.
  134. //-----------------------------------------------------------------------------
  135. VOID CMyD3DApplication::SetStatesForRecordingDepthComplexity()
  136. {
  137.     // Clear the stencil buffer
  138.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_STENCIL, 0x0, 1.0f, 0L );
  139.  
  140.     // Turn stenciling
  141.     m_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE,   TRUE );
  142.     m_pd3dDevice->SetRenderState( D3DRS_STENCILFUNC,     D3DCMP_ALWAYS );
  143.     m_pd3dDevice->SetRenderState( D3DRS_STENCILREF,      0 );
  144.     m_pd3dDevice->SetRenderState( D3DRS_STENCILMASK,     0x00000000 );
  145.     m_pd3dDevice->SetRenderState( D3DRS_STENCILWRITEMASK,0xffffffff );
  146.  
  147.     // Increment the stencil buffer for each pixel drawn
  148.     m_pd3dDevice->SetRenderState( D3DRS_STENCILZFAIL, D3DSTENCILOP_INCRSAT );
  149.     m_pd3dDevice->SetRenderState( D3DRS_STENCILFAIL,  D3DSTENCILOP_KEEP );
  150.     m_pd3dDevice->SetRenderState( D3DRS_STENCILPASS,  D3DSTENCILOP_INCRSAT );
  151. }
  152.  
  153.  
  154.  
  155.  
  156. //-----------------------------------------------------------------------------
  157. // Name: ShowDepthComplexity()
  158. // Desc: Draws the contents of the stencil buffer in false color. Use alpha
  159. //       blending of one red, one green, and one blue rectangle to do false
  160. //       coloring of bits 1, 2, and 4 in the stencil buffer.
  161. //-----------------------------------------------------------------------------
  162. VOID CMyD3DApplication::ShowDepthComplexity()
  163. {
  164.     // Turn off the buffer, and enable alpha blending
  165.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,          FALSE );
  166.     m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
  167.     m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCCOLOR );
  168.     m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR );
  169.  
  170.     // Set up the stencil states
  171.     m_pd3dDevice->SetRenderState( D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP );
  172.     m_pd3dDevice->SetRenderState( D3DRS_STENCILFAIL,  D3DSTENCILOP_KEEP );
  173.     m_pd3dDevice->SetRenderState( D3DRS_STENCILPASS,  D3DSTENCILOP_KEEP );
  174.     m_pd3dDevice->SetRenderState( D3DRS_STENCILFUNC,  D3DCMP_NOTEQUAL );
  175.     m_pd3dDevice->SetRenderState( D3DRS_STENCILREF,   0 );
  176.  
  177.     // Set the background to black
  178.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, 0x00000000, 1.0f, 0L );
  179.  
  180.     // Set render states for drawing a rectangle that covers the viewport.
  181.     // The color of the rectangle will be passed in D3DRS_TEXTUREFACTOR
  182.     m_pd3dDevice->SetVertexShader( D3DFVF_XYZRHW );
  183.     m_pd3dDevice->SetStreamSource( 0, m_pBigSquareVB, sizeof(D3DXVECTOR4) );
  184.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TFACTOR );
  185.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
  186.  
  187.     // Draw a red rectangle wherever the 1st stencil bit is set
  188.     m_pd3dDevice->SetRenderState( D3DRS_STENCILMASK, 0x01 );
  189.     m_pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, 0xffff0000 );
  190.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  191.  
  192.     // Draw a green rectangle wherever the 2nd stencil bit is set
  193.     m_pd3dDevice->SetRenderState( D3DRS_STENCILMASK, 0x02 );
  194.     m_pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, 0xff00ff00 );
  195.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  196.  
  197.     // Draw a blue rectangle wherever the 3rd stencil bit is set
  198.     m_pd3dDevice->SetRenderState( D3DRS_STENCILMASK, 0x04 );
  199.     m_pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, 0xff0000ff );
  200.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  201.  
  202.     // Restore states
  203.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  204.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  205.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  206.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,          TRUE );
  207.     m_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE,    FALSE );
  208.     m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
  209. }
  210.  
  211.  
  212.  
  213.  
  214. //-----------------------------------------------------------------------------
  215. // Name: Render()
  216. // Desc: Called once per frame, the call is the entry point for 3d
  217. //       rendering. This function sets up render states, clears the
  218. //       viewport, and renders the scene.
  219. //-----------------------------------------------------------------------------
  220. HRESULT CMyD3DApplication::Render()
  221. {
  222.     // Clear the viewport
  223.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0x000000ff,
  224.                    1.0f, 0L );
  225.  
  226.     // Begin the scene
  227.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  228.     {
  229.         if( m_bShowDepthComplexity )
  230.             SetStatesForRecordingDepthComplexity();
  231.  
  232.         // Render the scene
  233.         m_pFileObject->Render( m_pd3dDevice, &m_matWorldMatrix );
  234.  
  235.         // Show the depth complexity of the scene
  236.         if( m_bShowDepthComplexity )
  237.             ShowDepthComplexity();
  238.  
  239.         // Output statistics
  240.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  241.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  242.  
  243.         // End the scene.
  244.         m_pd3dDevice->EndScene();
  245.     }
  246.  
  247.     return S_OK;
  248. }
  249.  
  250.  
  251.  
  252.  
  253. //-----------------------------------------------------------------------------
  254. // Name: InitDeviceObjects()
  255. // Desc: Initialize scene objects.
  256. //-----------------------------------------------------------------------------
  257. HRESULT CMyD3DApplication::InitDeviceObjects()
  258. {
  259.     // Initialize the font's internal textures
  260.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  261.  
  262.     // Load a .X file
  263.     if( FAILED( m_pFileObject->Create( m_pd3dDevice, _T("Heli.x") ) ) )
  264.         return D3DAPPERR_MEDIANOTFOUND;
  265.  
  266.     // Create a big square for rendering the stencilbuffer contents
  267.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(D3DXVECTOR4),
  268.                                          D3DUSAGE_WRITEONLY, D3DFVF_XYZRHW,
  269.                                          D3DPOOL_MANAGED, &m_pBigSquareVB ) ) )
  270.         return E_FAIL;
  271.  
  272.     return S_OK;
  273. }
  274.  
  275.  
  276.  
  277.  
  278. //-----------------------------------------------------------------------------
  279. // Name: RestoreDeviceObjects()
  280. // Desc: Initialize scene objects.
  281. //-----------------------------------------------------------------------------
  282. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  283. {
  284.     HRESULT hr;
  285.  
  286.     m_pFont->RestoreDeviceObjects();
  287.  
  288.     if( FAILED( hr = m_pFileObject->RestoreDeviceObjects( m_pd3dDevice ) ) )
  289.         return hr;
  290.  
  291.     // Setup textures (the .X file may have textures)
  292.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  293.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  294.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  295.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  296.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  297.  
  298.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   TRUE );
  299.     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
  300.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
  301.     m_pd3dDevice->SetRenderState( D3DRS_COLORVERTEX,    TRUE );
  302.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0x00000000 );
  303.  
  304.     // Set the transform matrices
  305.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, 0.0f,-15.0f );
  306.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f,  0.0f );
  307.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f,  0.0f );
  308.     D3DXMATRIX  matWorld, matView, matProj;
  309.  
  310.     D3DXMatrixIdentity( &m_matWorldMatrix );
  311.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  312.     FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  313.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
  314.  
  315.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  316.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  317.  
  318.     // Setup a material
  319.     D3DMATERIAL8 mtrl;
  320.     D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f );
  321.     m_pd3dDevice->SetMaterial( &mtrl );
  322.  
  323.     D3DLIGHT8 light;
  324.     D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f, -1.0f, 0.0f );
  325.     m_pd3dDevice->SetLight( 0, &light );
  326.     m_pd3dDevice->LightEnable( 0, TRUE );
  327.     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
  328.  
  329.     // Resize the quad used to render the stencilbuffer contents
  330.     D3DXVECTOR4* v;
  331.     FLOAT sx = (FLOAT)m_d3dsdBackBuffer.Width;
  332.     FLOAT sy = (FLOAT)m_d3dsdBackBuffer.Height;
  333.     m_pBigSquareVB->Lock( 0, 0, (BYTE**)&v, 0 );
  334.     v[0] = D3DXVECTOR4(  0, sy, 0.0f, 1.0f );
  335.     v[1] = D3DXVECTOR4(  0,  0, 0.0f, 1.0f );
  336.     v[2] = D3DXVECTOR4( sx, sy, 0.0f, 1.0f );
  337.     v[3] = D3DXVECTOR4( sx,  0, 0.0f, 1.0f );
  338.     m_pBigSquareVB->Unlock();
  339.  
  340.     return S_OK;
  341. }
  342.  
  343.  
  344.  
  345.  
  346. //-----------------------------------------------------------------------------
  347. // Name: InvalidateDeviceObjects()
  348. // Desc:
  349. //-----------------------------------------------------------------------------
  350. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  351. {
  352.     m_pFont->InvalidateDeviceObjects();
  353.     m_pFileObject->InvalidateDeviceObjects();
  354.  
  355.     return S_OK;
  356. }
  357.  
  358.  
  359.  
  360.  
  361. //-----------------------------------------------------------------------------
  362. // Name: DeleteDeviceObjects()
  363. // Desc: Called when the app is exiting, or the device is being changed,
  364. //       this function deletes any device dependent objects.
  365. //-----------------------------------------------------------------------------
  366. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  367. {
  368.     m_pFont->DeleteDeviceObjects();
  369.     m_pFileObject->Destroy();
  370.  
  371.     SAFE_RELEASE( m_pBigSquareVB );
  372.  
  373.     return S_OK;
  374. }
  375.  
  376.  
  377.  
  378.  
  379. //-----------------------------------------------------------------------------
  380. // Name: FinalCleanup()
  381. // Desc: Called before the app exits, this function gives the app the chance
  382. //       to cleanup after itself.
  383. //-----------------------------------------------------------------------------
  384. HRESULT CMyD3DApplication::FinalCleanup()
  385. {
  386.     SAFE_DELETE( m_pFont );
  387.     SAFE_DELETE( m_pFileObject );
  388.  
  389.     return S_OK;
  390. }
  391.  
  392.  
  393.  
  394.  
  395. //-----------------------------------------------------------------------------
  396. // Name: ConfirmDevice()
  397. // Desc: Called during device intialization, this code checks the device
  398. //       for some minimum set of capabilities
  399. //-----------------------------------------------------------------------------
  400. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  401.                                           D3DFORMAT Format )
  402. {
  403.     // Make sure device supports directional lights
  404.     if( (dwBehavior & D3DCREATE_HARDWARE_VERTEXPROCESSING ) ||
  405.         (dwBehavior & D3DCREATE_MIXED_VERTEXPROCESSING ) )
  406.     {
  407.         if( 0 == ( pCaps->VertexProcessingCaps & D3DVTXPCAPS_DIRECTIONALLIGHTS ) )
  408.             return E_FAIL;
  409.     }
  410.  
  411.     return S_OK;
  412. }
  413.  
  414.  
  415.  
  416.  
  417. //-----------------------------------------------------------------------------
  418. // Name: MsgProc()
  419. // Desc: Overrrides the main WndProc, so the sample can do custom message
  420. //       handling (e.g. processing mouse, keyboard, or menu commands).
  421. //-----------------------------------------------------------------------------
  422. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  423.                                     LPARAM lParam )
  424. {
  425.     if( WM_COMMAND == uMsg )
  426.     {
  427.         switch( LOWORD(wParam) )
  428.         {
  429.             case IDM_SHOWDEPTHCOMPLEXITY:
  430.                 m_bShowDepthComplexity = !m_bShowDepthComplexity;
  431.                 CheckMenuItem( GetMenu(hWnd), IDM_SHOWDEPTHCOMPLEXITY,
  432.                           m_bShowDepthComplexity ? MF_CHECKED : MF_UNCHECKED );
  433.                 break;
  434.         }
  435.     }
  436.  
  437.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  438. }
  439.  
  440.  
  441.  
  442.